package com.gugee.tiktok.common.model.dal.blogger;

import com.gugee.tiktok.common.model.mysql.Aweme;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;


@Slf4j
public class AwemeExtRepositoryImpl implements AwemeExtRepository {

    @Autowired
    @Lazy
    AwemeRepository awemeRepository;

    @Autowired
    JdbcTemplate jdbcTemplate;

    private Optional<Aweme> insertOrUpdateByCondition(Aweme aweme, boolean isSkipUpdate, String operator) {
        // lock here
        var optionalAweme = awemeRepository.findByoAwemeId(aweme.getOAwemeId());

        if (!optionalAweme.isPresent()) {
            aweme.setCreateBy(operator);
            aweme.setUpdateBy(operator);
            awemeRepository.save(aweme);
        } else if (optionalAweme.isPresent() && !isSkipUpdate) {
            var origin = optionalAweme.get();
            aweme.setId(origin.getId());
            aweme.setCreateTime(origin.getCreateTime());
            aweme.setCreateBy(origin.getCreateBy());
            aweme.setUpdateBy(operator);
            awemeRepository.save(aweme);
        }

        return optionalAweme;
    }

    @Override
    public Optional<Aweme> insertOrUpdateAndReturnOriginAweme(Aweme aweme, String operator) {
        return insertOrUpdateByCondition(aweme, false, operator);
    }
    @Override
    public void batchSave(List<Aweme> awemes) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate(
                "INSERT INTO `tiktok`.`aweme`" +
                        "(`id`, `o_aweme_id`, `o_desc`, `o_create_time`, `o_uid`, `o_music_id`," +
                        " `o_comment_count`, `o_digg_count`,`o_download_count`, `o_play_count`, `o_share_count`, `o_whatsapp_share_count`, " +
                        "`o_forward_count`, `o_rate`, `o_blogger_nick_name`, `o_blogger_unique_id`, `o_text_extra`, `o_share_url`, " +
                        "`o_duration`, `o_region`, `o_is_ads`, `o_cover`, `o_dynamic_cover`, `create_time`, " +
                        "`update_time`, `create_by`, `update_by`, `datecode`, `score`, `blogger_type`, `anchor`,`last_aweme_cl_time`)" +
                        " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Aweme aweme = awemes.get(i);
                        preparedStatement.setLong(1, 0);
                        preparedStatement.setLong(2, aweme.getOAwemeId());
                        preparedStatement.setString(3, aweme.getODesc());
                        preparedStatement.setLong(4, aweme.getOCreateTime());
                        preparedStatement.setLong(5, aweme.getOUid());
                        preparedStatement.setLong(6, aweme.getOMusicId());
                        preparedStatement.setInt(7, aweme.getOCommentCount());
                        preparedStatement.setLong(8, aweme.getODiggCount());
                        preparedStatement.setInt(9, aweme.getODownloadCount());
                        preparedStatement.setLong(10, aweme.getOPlayCount());
                        preparedStatement.setLong(11, aweme.getOShareCount());
                        preparedStatement.setInt(12, aweme.getOWhatsappShareCount());
                        preparedStatement.setInt(13, aweme.getOForwardCount());
                        preparedStatement.setLong(14, aweme.getORate());
                        preparedStatement.setString(15, aweme.getOBloggerNickName());
                        preparedStatement.setString(16, aweme.getOBloggerUniqueId());
                        preparedStatement.setString(17, aweme.getOTextExtra());
                        preparedStatement.setString(18, aweme.getOShareUrl());
                        preparedStatement.setInt(19, aweme.getODuration());
                        preparedStatement.setString(20, aweme.getORegion());
                        preparedStatement.setBoolean(21, aweme.isOIsAds());
                        preparedStatement.setString(22, aweme.getOCover());
                        preparedStatement.setString(23, aweme.getODynamicCover());
                        preparedStatement.setLong(24, seconds);
                        preparedStatement.setLong(25, seconds);
                        preparedStatement.setString(26, "batch save");
                        preparedStatement.setString(27, "batch save");
                        preparedStatement.setInt(28, aweme.getDatecode());
                        preparedStatement.setDouble(29, aweme.getScore());
                        preparedStatement.setString(30, aweme.getBloggerType());
                        preparedStatement.setString(31, aweme.getAnchor());
                        preparedStatement.setLong(32, seconds);
                    }

                    @Override
                    public int getBatchSize() {
                        return awemes.size();
                    }
                });
    }
    @Override
    public void batchUpdateType(List<Aweme> awemes) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate(
                "UPDATE `tiktok`.`aweme` SET  " +
                        " `last_aweme_type_time` = ?,  `update_by` = ?, `aweme_type` = ?, `blogger_type` = ? , `aweme_type_source` = ?, `aweme_type_comments` = ?" +
                        " WHERE `id` = ? "
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Aweme aweme = awemes.get(i);

                        preparedStatement.setLong(1, aweme.getLastAwemeTypeTime());
                        preparedStatement.setString(2, "awemeType&bloggerType batchUpdate");
                        preparedStatement.setString(3, aweme.getAwemeType());
                        preparedStatement.setString(4, aweme.getBloggerType());
                        preparedStatement.setInt(5,aweme.getAwemeTypeSource());
                        preparedStatement.setInt(6,aweme.getAwemeTypeComments());
                        preparedStatement.setLong(7, aweme.getId());

                    }
                    @Override
                    public int getBatchSize() {
                        return awemes.size();
                    }
                });
    }
    @Override
    public void batchUpdate(List<Aweme> awemes) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate(
                "UPDATE `tiktok`.`aweme` SET  " +
                        " `o_music_id` = ?, `o_comment_count` = ?, `o_digg_count` = ?, `o_download_count` = ?, `o_play_count` = ?," +
                        " `o_share_count` = ?, `o_whatsapp_share_count` = ?, `o_forward_count` = ?, `o_rate` = ?, `o_blogger_nick_name` = ?, " +
                        "`o_blogger_unique_id` = ?, `o_duration` = ?, `o_region` = ?, `o_is_ads` = ?," +
                        " `update_time` = ?,  `score` = ?, `anchor` = ?,`last_aweme_cl_time` = ?" +
                        " WHERE `datecode` = ? and `id` = ?"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Aweme aweme = awemes.get(i);
                        preparedStatement.setLong(1, aweme.getOMusicId());
                        preparedStatement.setInt(2, aweme.getOCommentCount());
                        preparedStatement.setLong(3, aweme.getODiggCount());
                        preparedStatement.setInt(4, aweme.getODownloadCount());
                        preparedStatement.setLong(5, aweme.getOPlayCount());
                        preparedStatement.setInt(6, aweme.getOShareCount());
                        preparedStatement.setInt(7, aweme.getOWhatsappShareCount());
                        preparedStatement.setInt(8, aweme.getOForwardCount());
                        preparedStatement.setInt(9, aweme.getORate());
                        preparedStatement.setString(10, aweme.getOBloggerNickName());
                        preparedStatement.setString(11, aweme.getOBloggerUniqueId());
                        preparedStatement.setLong(12, aweme.getODuration());
                        preparedStatement.setString(13, aweme.getORegion());
                        preparedStatement.setBoolean(14, aweme.isOIsAds());
                        preparedStatement.setLong(15, seconds);
                        preparedStatement.setDouble(16, aweme.getScore());
                        preparedStatement.setString(17, aweme.getAnchor());
                        preparedStatement.setLong(18, seconds);
                        preparedStatement.setLong(19, aweme.getDatecode());
                        preparedStatement.setLong(20, aweme.getId());



                    }
                    @Override
                    public int getBatchSize() {
                        return awemes.size();
                    }
                });
    }

}
